package com.example.xwweibo.api;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.xwcommon.entity.Comment;
import com.example.xwcommon.entity.User;
import com.example.xwcommon.entity.Weibo;
import com.example.xwcommon.service.CommentService;
import com.example.xwcommon.service.UserService;
import com.example.xwcommon.service.WeiboService;
import com.example.xwcommon.vo.Response;
import io.seata.core.context.RootContext;
import io.seata.core.exception.TransactionException;
import io.seata.spring.annotation.GlobalTransactional;
import io.seata.tm.api.GlobalTransactionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
public class CommentApi {
    @Autowired
    @Qualifier("commentService")
    private CommentService commentService;
    @Qualifier("com.example.xwcommon.service.UserService")
    @Autowired
    private UserService userService;
    @Autowired
    @Qualifier("weiboService")
    private WeiboService weiboService;

    @RequestMapping("/api/comment/addComment")
    @GlobalTransactional
    public ResponseEntity<Response<Comment>> addComment(Integer wbid, String content, String userloginname, HttpSession session) throws TransactionException {
        try{
            Response<Comment> response = commentService.addComment(wbid, content, userloginname);
            if(response.getCode() == 200){
                User user =userService.getUserByLoginName(userloginname).getData();
                if(userService.addScore(user.getLoginName(),1).getCode()==200){
                    return ResponseEntity.ok(response);
                }
            }
        }catch (RuntimeException e){
            GlobalTransactionContext.reload(RootContext.getXID()).rollback();
            return ResponseEntity.status(500).body(new Response<>(500,"参数错误",null));
        }
        return ResponseEntity.status(500).body(new Response<>(500,"参数错误",null));
    }

    @RequestMapping("/api/comment/showListByWeibo")
    public ResponseEntity<Response<Page<Comment>>> showListByWeibo(Integer wbid, Integer pageNum, Integer pageSize, Integer state) {
        Response<Page<Comment>> response = commentService.showListByWeibo(wbid, pageNum, pageSize, state);
        if(response.getCode()==200){
            return ResponseEntity.ok(response);
        }
        return ResponseEntity.status(500).body(new Response<>(500,"参数错误",null));
    }
    @RequestMapping("/api/comment/showListByUser")
    public ResponseEntity<Response<Page<Comment>>> showListByUser(String userloginname, Integer pageNum, Integer pageSize, Integer state) {
        Response<Page<Comment>> response = commentService.showListByUser(userloginname, pageNum, pageSize, state);
        if(response.getCode()==200){
            return ResponseEntity.ok(response);
        }
        return ResponseEntity.status(500).body(new Response<>(500,"参数错误",null));
    }
    @RequestMapping("/api/comment/removeComment")
    @GlobalTransactional
    public ResponseEntity<Response<Integer>> removeComment(Integer cmid) throws TransactionException {
        try{
            Response<Integer> response = commentService.removeComment(cmid);
            if(response.getCode() == 200){
                User user =userService.getUserByLoginName(userService.getUserById(cmid).getData().getLoginName()).getData();
                if(userService.addScore(user.getLoginName(),-1).getCode()==200){
                    return ResponseEntity.ok(response);
                }
            }
        }catch (RuntimeException e){
            GlobalTransactionContext.reload(RootContext.getXID()).rollback();
            return ResponseEntity.status(500).body(new Response<>(500,"参数错误",null));
        }
        return ResponseEntity.status(500).body(new Response<>(500,"参数错误",null));
    }
    @RequestMapping("/api/comment/updateCommentState")
    public ResponseEntity<Response<Comment>> updateCommentState(Integer commentId, Integer state) {
        Response<Comment> response = commentService.updateState(commentId, state);
        if(response.getCode()==200){
            return ResponseEntity.ok(response);
        }
        return ResponseEntity.status(500).body(new Response<>(500,"参数错误",null));
    }
}
